package com.jz.yanshe.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jz.jd.rest.response.StyleResponse;

import java.util.List;

import com.jz.yanshe.R;


/**
 * Created by Administrator on 2018/3/1.
 */

public class TypeAdapter extends RecyclerView.Adapter<TypeAdapter.Holder> {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<StyleResponse.Sizes> mList;
    public OnItemClick onItemClick;


    public TypeAdapter(Context context, List list) {
        mList = list;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        Holder myHolder = new Holder(mLayoutInflater.inflate(R.layout.item_textview, parent, false));
        return myHolder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
//            System.out.println(list.get(position).isSelected   + "   "   +  position);
//            holder.radio.setChecked(list.get(position).isSelected);
        if(mList.get(position).select == 0){
            holder.tv_name.setBackgroundResource(R.drawable.item_bg_grey);
            holder.tv_name.setTextColor(Color.parseColor("#4d4d4d"));
        }
        if(mList.get(position).select == 1){
            holder.tv_name.setBackgroundResource(R.drawable.item_bg_red);
            holder.tv_name.setTextColor(Color.parseColor("#E61717"));
        }
        if(mList.get(position).select == -1){
            holder.tv_name.setBackgroundResource(R.color.maskGrey);
            holder.tv_name.setTextColor(Color.parseColor("#66808080"));
        }

        if (mList.get(position).style_color == null) {
            holder.tv_name.setText(mList.get(position).style_size);
        } else {
            holder.tv_name.setText(mList.get(position).style_color);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mList.get(position).select == 0) {
                    for (int i = 0; i < mList.size(); i++) {
                        if (mList.get(i).select != -1) {
                            mList.get(i).select = 0;
                        }
                    }
                    mList.get(position).select = 1;
                    notifyDataSetChanged();

                    if (mList.get(position).style_color == null) {
                        onItemClick.onItemClick(mList.get(position).style_size);
                    }
                    if(mList.get(position).style_size == null){
                        onItemClick.onItemClick(mList.get(position).style_color);
                    }
                    return;
                }
                if (mList.get(position).select == 1) {
                    holder.tv_name.setBackgroundResource(R.drawable.item_bg_grey);
                    holder.tv_name.setTextColor(Color.parseColor("#4d4d4d"));
                    mList.get(position).select = 0;
                }
            }
        });
//            holder.radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                    if(isChecked == true){
//                        for(int i = 0;i<list.size();i++){
//                            list.get(i).isSelected = false;
//                        }
//                        list.get(position).isSelected = true;
//                        notifyDataSetChanged();
//                    }
//                }
//            });
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class Holder extends RecyclerView.ViewHolder {

        TextView tv_name;
//        RadioButton radio;

        public Holder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
//            radio = itemView.findViewById(R.id.radio);
        }
    }

    public interface OnItemClick{

        void onItemClick(String style_color);

        void onItemClick();
    }


}
